﻿/*
* author: xxxgggyyy
* date	: 2022-10-31
* desc	: BPT的叶子节点
*/
#ifndef BPT_LEAF_H
#define BPT_LEAF_H

#include "bpt_node.h"

class BPTLeaf : public BPTNode {
	friend class BPTree;
	friend class BPTNode;
private:
	//vector<unsigned long> keys;
	//vector<unsigned long> vals;
	int capacity;
	//unsigned long* keys;
	unsigned long* vals;
	BPTLeaf* sibling;

protected:
	BPTLeaf(int size);
	//BPTLeaf();
	~BPTLeaf();

	bool insert(unsigned long, unsigned long);
	void __insert(int, unsigned long, unsigned long);
	bool is_leaf();
	void print();
	BPTLeaf* split(unsigned long&);
	bool spilled();
	bool remove(unsigned long);
	void __remove(int);
	bool concealed();
	bool concealed(int);
	unsigned long borrow(BPTNode*, unsigned long, bool);
	void append(BPTNode*, unsigned long key);
	bool contain(unsigned long);

	/*序列化格式说明-按字节顺序
	* sizeof(int)			: 序列化长度
	* 1						: 标志字节
	* sizeof(int)			: size字段
	* sizeof(ulong)*size	: keys数组
	* sizeof(ulong)*size	: vals数组
	* sizeof(int)			: capacity字段
	* 
	* sibling字段不记录，构造树时自动连接
	*/

	/*手动序列化，也可直接使用fstream直接写入但速度稍慢*/
	char* serialize(int&);
	bool load(char*);

};

#endif // !BPT_LEAF_H

